import { message } from 'antd';
import { updateAccountStatus, getUserBroadDetail, getUserBroadList} from '../services/api';

export default {
  namespace: 'userBroad',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    userBroadInfo: {},
    userBroadList: [],
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(getUserBroadList, payload);
      yield put({
        type: 'userBroadLists',
        payload: response,
      });
    },
    *getUserBroadDetail({ payload }, { call, put }) {
      const response = yield call(getUserBroadDetail, payload);
      yield put({
        type: 'userBroadInfos',
        payload: response,
      });
    },

    *updateAccountStatus({ payload, callback }, { call }) {
      const response = yield call(updateAccountStatus, payload);
      if (response.code === 0) {
        message.success('更改成功');
      } else {
        message.error('更改失败');
      }
      if (callback) callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    userBroadLists(state, action) {
      return {
        ...state,
        userBroadList: action.payload.data,
      };
    },
    userBroadInfos(state, action) {
      return {
        ...state,
        userBroadInfo: action.payload.data || {},
      };
    },
  },
};
